﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;
using SYcwpb.Helpers;
using SYcwpb.ViewModels;
using SYcwpb.ViewModels.DataProvider;
using System.Data;
using SYcwpb.Commands;
using System.ComponentModel;

namespace SYcwpb.Views
{
    /// <summary>
    /// BedRoomsView.xaml 的交互逻辑
    /// </summary>
    public partial class BedRoomsView : Window
    {
        private static BedRoomsView _view = null;

        private BedRoomsViewModel _viewModel = null;


        private GroupStyle _groupStyle;

        private bool canInsert = false;

        private SqlDataLayer sqlHelper = new SqlDataLayer();

        public BedRoomsView()
        {
            InitializeComponent();

            _groupStyle = (GroupStyle)dg_rooms.FindResource("gs_Default");
        }

        public static BedRoomsView GetInstances()
        {
            if (null == _view || WindowHelper.IsDisposed(_view))
                _view = new BedRoomsView();

            return _view;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                _viewModel = new BedRoomsViewModel();

                this.DataContext = _viewModel;

                dg_rooms.GroupStyle.Add(_groupStyle);
                dg_rooms.Items.GroupDescriptions.Add(new PropertyGroupDescription("RoomName"));

                //dg_rooms.Items.SortDescriptions.Add(new SortDescription("BedIndex", ListSortDirection.Ascending));
                canInsert = true;

                //公寓房间编辑
                this.cmb_RoomType.SelectedIndex = 0;
                this.cmb_BedCount.SelectedIndex = 0;

                this.cb_add.Checked += new RoutedEventHandler(IS_Checked);
                this.cb_delete.Checked += new RoutedEventHandler(IS_Checked);
                this.cb_modify.Checked += new RoutedEventHandler(IS_Checked);
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowError(ex);
            }

        }

        private void IS_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            switch (cb.Name)
            {
                case "cb_add":
                    this.cb_delete.IsChecked = false;
                    this.cb_modify.IsChecked = false;
                    this.cmb_RoomNum.IsEditable = true;
                    this.cmb_Priority.IsEnabled = true;
                    this.cmb_BedCount.IsEnabled = true;
                    this.cmb_RoomType.IsEnabled = true;


                    this.tb_RoomPhone.Text = "";
                    this.tb_RoomPhone.IsEnabled = true;
                    if (_viewModel.BedRoomCollections != null)
                    {
                        _viewModel.BedRoomCollections = null;
                    }
                    break;
                case "cb_delete":
                    this.cb_modify.IsChecked = false;
                    this.cb_add.IsChecked = false;
                    this.cmb_Priority.IsEnabled = false;
                    this.tb_RoomPhone.IsEnabled = false;
                    this.cmb_RoomType.IsEnabled = false;
                    this.cmb_RoomNum.IsEnabled = true;
                    this.cmb_RoomNum.IsEditable = false;
                    this.cmb_BedCount.IsEnabled = false;
                    if (_viewModel.BedRoomCollections != null)
                    {
                        _viewModel.BedRoomCollections = null;
                    }
                    _viewModel.InitBedRoomObjects();
                    break;
                case "cb_modify":
                    this.cb_add.IsChecked = false;
                    this.cb_delete.IsChecked = false;
                    this.cmb_RoomNum.IsEditable = false;
                    this.tb_RoomPhone.IsEnabled = true;
                    this.cmb_Priority.IsEnabled = true;
                    this.cmb_RoomType.IsEnabled = true;
                    this.cmb_RoomNum.IsEnabled = true;
                    this.cmb_BedCount.IsEnabled = true;

                    if (_viewModel.BedRoomCollections != null)
                    {
                        _viewModel.BedRoomCollections = null;
                    }
                    _viewModel.InitBedRoomObjects();

                    break;
            }
        }
        public string GetDutyTime()
        {
            return GlobalVariables.FormatTime(this.DutyTime.GetTimeSecond().ToString());
        }

        public string GetCallTime()
        {
            return GlobalVariables.FormatTime(this.CallTime.GetTimeSecond().ToString(), true);
        }

        private void DatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            _viewModel.InitBedRoomObjects();
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            BedRoomObject bed = dg_rooms.SelectedItem as BedRoomObject;

            if (bed.TheDriver != null)
            {
                // bed.TheDriver.DutyTime 

                //if (!string.IsNullOrEmpty(bed.TheDriver.DriverName) && !string.IsNullOrEmpty(bed.TheDriver.DutyTime))
                //{
                //    _viewModel.CancelCall(bed.TheDriver.DriverName,Convert.ToDateTime(bed.TheDriver.DutyTime).ToString("HH:mm"));
                //}

                bed.TheDriver = null;

            }

        }

        private void cancelCallUp_Click(object sender, RoutedEventArgs e)
        {
            BedRoomObject bed = dg_rooms.SelectedItem as BedRoomObject;

            if (bed.TheDriver != null && bed.CanCelCall == BedRoomObject.CANCEL_CALL)
            {
                // bed.TheDriver.DutyTime 

                if (!string.IsNullOrEmpty(bed.TheDriver.DriverName) && !string.IsNullOrEmpty(bed.TheDriver.DutyTime))
                {
                    if (_viewModel.CancelCall(bed))
                    {
                        MessageBoxHelper.ShowTips("取消成功！");
                    }
                    else
                    {
                        MessageBoxHelper.ShowTips("取消失败！");
                    }
                }



            }
        }

        private void CallUpByPBY_Click(object sender, RoutedEventArgs e)
        {
            BedRoomObject bed = dg_rooms.SelectedItem as BedRoomObject;

            if (bed.TheDriver != null)
            {
                // bed.TheDriver.DutyTime 

                if (!string.IsNullOrEmpty(bed.TheDriver.DriverName) && !string.IsNullOrEmpty(bed.TheDriver.DutyTime))
                {
                    // _viewModel.CancelCall(bed.TheDriver.DriverName, Convert.ToDateTime(bed.TheDriver.DutyTime).ToString("HH:mm"));

                }



            }
        }

        private void btn_Sure_Click(object sender, RoutedEventArgs e)
        {
            //添加
            if (this.cb_add.IsChecked.ToString() == "True")
            {
                string roomNum = this.cmb_RoomNum.Text.Trim().ToString();
                if (roomNum == "")
                {
                    MessageBoxHelper.ShowTips("请输入房间编号！");
                    return;
                }
                string sql = "select * from BedRooms where br_name = '" + roomNum + "'";
                DataTable dt = sqlHelper.SelectDt(sql);
                if (dt.Rows.Count > 0)
                {
                    MessageBoxHelper.ShowTips("该房间编号已经存在，请重新输入房间编号！");
                    return;
                }
                if (this.tb_RoomPhone.Text.Trim().ToString() == "")
                {
                    MessageBoxHelper.ShowTips("请输入电话号码！");
                    return;
                }
                sql = "select * from BedRooms where br_phoneno = '" + this.tb_RoomPhone.Text.Trim().ToString() + "'";
                dt = sqlHelper.SelectDt(sql);
                if (dt.Rows.Count > 0)
                {
                    MessageBoxHelper.ShowTips("该电话号码已经存在，请重新输入电话号码！");
                    return;
                }

                sql = "insert into BedRooms values('" + this.cmb_RoomNum.Text.Trim().ToString() + "','" + this.cmb_BedCount.Text.ToString()
                    + "','" + this.cmb_BedCount.Text.ToString() + "','" + this.cmb_RoomType.Text.ToString() + "','" + Convert.ToInt32(CONSTDATA.UserModel.Line) + "','" + this.cmb_Priority.Text.ToString()
                    + "','" + this.tb_RoomPhone.Text.Trim().ToString() + "')";
                if (sqlHelper.ExecuteSql(sql) > 0)
                {
                    MessageBoxHelper.ShowTips("添加房间成功！");
                }
                else
                {
                    MessageBoxHelper.ShowError("添加房间失败！");
                }
            }

            //删除
            else if (this.cb_delete.IsChecked.ToString() == "True")
            {
                string roomNum = this.cmb_RoomNum.Text.Trim().ToString();
                if (roomNum == "")
                {
                    MessageBoxHelper.ShowTips("请选择您想删除的房间号！");
                    return;
                }
                string sql = "delete from BedRoomMaps  where brm_br_name = '" + roomNum + "' and brm_date ='" +
                    DateTime.Now.ToString("yyyy-MM-dd") + "';delete from BedRooms where br_name ='" + roomNum + "'";
                if (sqlHelper.ExecuteSql(sql) > 0)
                {
                    _viewModel.InitBedRoomObjects();
                    MessageBoxHelper.ShowTips("删除房间" + roomNum + "成功！");
                }
                else
                {
                    MessageBoxHelper.ShowError("删除房间" + roomNum + "失败！");
                }

            }

            //修改
            else if (this.cb_modify.IsChecked.ToString() == "True")
            {
                if (this.cmb_RoomNum.Text.Trim().ToString() == "")
                {
                    MessageBoxHelper.ShowTips("请选择需要修改的房间！");
                    return;
                }
                else if (this.tb_RoomPhone.Text.Trim().ToString() == "")
                {
                    MessageBoxHelper.ShowTips("请输入电话号码！");
                    return;
                }
                else if (this.cmb_Priority.Text.Trim().ToString() == "")
                {
                    MessageBoxHelper.ShowTips("请选择优先级！");
                    return;
                }
                else
                {
                    string sql = "update  BedRooms set br_bed_avai='" + this.cmb_BedCount.Text.ToString() + "',br_sex='"
                        + this.cmb_RoomType.Text.ToString() + "',br_line='" + Convert.ToInt32(CONSTDATA.UserModel.Line)
                        + "',br_priority='" + this.cmb_Priority.Text.ToString() + "',br_phoneno='"
                        + this.tb_RoomPhone.Text.Trim().ToString() + "' where br_name ='" + this.cmb_RoomNum.Text.ToString() + "'";
                    if (sqlHelper.ExecuteSql(sql) > 0)
                    {
                        _viewModel.InitBedRoomObjects();
                        MessageBoxHelper.ShowTips("修改成功！");
                    }
                    else
                    {
                        MessageBoxHelper.ShowTips("修改失败！");

                    }
                }
            }

            else
            {
                MessageBoxHelper.ShowTips("请选择修改、添加或删除操作！");
            }
        }

        private void cmb_RoomNum_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                string roomNumber = ((SYcwpb.Models.BedRoomsModel)(((System.Windows.Controls.Primitives.Selector)(sender)).SelectedValue)).RoomName;
                string sql = "select * from BedRooms where br_name = '" + roomNumber + "'";
                DataTable temp = sqlHelper.SelectDt(sql);
                if (temp.Rows.Count > 0)
                {
                    this.cmb_BedCount.Text = temp.Rows[0]["br_bed_avai"].ToString();
                    this.cmb_RoomType.Text = temp.Rows[0]["br_sex"].ToString();
                    this.tb_RoomPhone.Text = temp.Rows[0]["br_phoneno"].ToString();
                    this.cmb_Priority.Text = temp.Rows[0]["br_priority"].ToString();
                }
                else
                {
                    return;
                }
            }
            catch (Exception ex)
            {

            }

        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {

            if (_viewModel != null)
            {
                if (!_viewModel.IsExporting)
                {
                    MessageBoxHelper.ShowTips("无法关闭窗口：请等候操作完成后重试！");
                    e.Cancel = true;
                }
            }
        }

        private void dg_rooms_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            e.Cancel = true;
        }

        private void dg_rooms_Sorting(object sender, DataGridSortingEventArgs e)
        {

        }

        private void dg_rooms_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void dg_rooms_SourceUpdated(object sender, DataTransferEventArgs e)
        {

        }

    }
}
